{include file="common_top.html"}
	{include file="common_head.html"}
	<style type="text/css">
	{literal}
		.datatable.list .data table input{width:100%;margin:2px 0px;}
		.datatable.list .data table .tr{text-align:right;}
		.uploaddialog{border:1px solid #CCCCCC;background:#FFFFFF;border-radius:4px 4px 4px 4px;}
		.uploaddialog .panel{border:3px dashed #DDDDDD;width:500px;height:100px;margin:0px;float:none;margin:10px;border-radius:4px 4px 4px 4px;}
		.uploaddialog .panel .info{color:#DDDDDD;font-size:26px;height:50px;margin:auto;bottom:0;left:0;right:0;top:0;position:absolute;text-align:center;}
		.uploaddialog .message{border-radius:4px 4px 4px 4px;background:#000000;color:#FFFFFF;padding:5px 10px;margin:5px;top:0;left:0;position:absolute;display:none;box-shadow:0 2px 4px rgba(0, 0, 0, 0.2);}
		.uploaddialog .loading{background-color:#9999FF;height:10px;width:480px;margin:20px auto;}
		.uploaddialog .loading .bar{background-image:url('/images/bar-loading.gif');width:0%;height:100%;opacity:0.12;}
	{/literal}
	</style>
</head>
<body class="mainbody">
	<div class="datatable">
		<div class="data"><table><tbody>
			<tr class="nobb"><td class="filter"></td><td class="w50">
				<input type="button" class="btn btn_import" value="导入字典" />
			</td><td class="w50">
				<input type="button" class="btn btn_export" value="导出字典" />
			</td><td class="w50">
				<input type="button" class="btn btn_clear" value="清空缓存" />
			</td></tr>
		</tbody></table></div>
	</div>
	<div class="datatable">
		<form action="/index.php" method="get">
			<input type="hidden" name="a" value="dict" />
			<input type="hidden" name="m" value="list" />
			<div class="data"><table><tbody>
				<tr class="nobb"><td class="filter">
					<label>英</label><input type="text" name="en" value="{$params.en}" class="txt w250 dict_en" />
					<label>中</label><input type="text" name="cn" value="{$params.cn}" class="txt w250 dict_cn" />
				</td><td class="w50">
					<input type="button" data-id="0" class="btn btn_edit" value="添加" />
				</td><td class="w50">
					<input type="submit" class="btn" value="搜索" />
				</td></tr>
			</tbody></table></div>
		</form>
	</div>
	<div class="datatable list">
		<div class="data"><table><tbody>
			{foreach from=$objects.data item=object}
				{include file="dict_list_block.html"}
			{/foreach}
			<tr class="pagination"><td colspan="2">
				<div class="navigation">
					<a href="/index.php?a=dict&m=list&page=1&{$objects.pager.params}">« 第一页</a>
					<a href="/index.php?a=dict&m=list&page={$objects.pager.prev}&{$objects.pager.params}">« 上一页</a>
					{for $i = $objects.pager.start; $i <= $objects.pager.end; $i++}
						{if $i == $objects.pager.current}
							<a class="number current" href="javascript:;">{$i}</a>
						{else}
							<a class="number" href="/index.php?a=dict&m=list&page={$i}&{$objects.pager.params}">{$i}</a>
						{/if}
					{/for}
					<a href="/index.php?a=dict&m=list&page={$objects.pager.next}&{$objects.pager.params}">下一页 »</a>
					<a href="/index.php?a=dict&m=list&page={$objects.pager.last}&{$objects.pager.params}">最后页 »</a>
				</div>
			</td></tr>
		</tbody></table></div>
	</div>
	<div class="msgdialog" title="确认"></div>
	<div class="msg"></div>
	<script type="text/javascript">
	{literal}
		$("body").on("click", ".btn_edit", function(){
			var id = $(this).data("id");
			var cn = $(this).parent().parent().find(".dict_cn").val();
			var en = id;
			if(id == 0){
				en = $(this).parent().parent().find(".dict_en").val();
			}
			var that = this;
			$(".msg").html("");
			$.ajax({type: "POST", dataType: "JSON", url: "/index.php?a=dict&m=edit&t=ajax", data: {id: id, cn: cn, en: en}, async: false, success: function(result){
				if(result.state){
					if(id > 0){
						$.fn.notificationShow($(".msg"), "编辑成功。", "success");
					}else{
						$(that).parent().parent().find(".dict_cn").val("");
						$(that).parent().parent().find(".dict_en").val("");
						$.fn.notificationShow($(".msg"), "添加成功。", "success");
						$(".datatable.list .data table tbody").prepend(result.data);
					}
				}else{
					$.fn.notificationShow($(".msg"), result.message, "error");
				}
			}, error: function(jqXHR, textStatus, errorThrown){
				$.fn.notificationShow($(".msg"), "AJAX请求出错[" + errorThrown + "].", "error");
			}});
		});
		$(".btn_import").click(function(){
			$.fn.dyDialog({
				locate: {type: "window", x: "middle", y: "middle", origin: "mm"}, 
				content: "<div class='uploaddialog'><div class='panel'><div class='info'>拖拽文件到此</div></div><div class='message'></div></div>",
				close: {className: "close", bgBtn: true},
				onEvent: {open: function(){
					var obj = $(".uploaddialog .panel");
					obj.get(0).addEventListener("drop", H_Drop, false);
					obj.bind("dragleave", H_DragLeave);
					obj.bind("dragover", H_DragOver);
				}}
			});
		});
		$(".btn_export").click(function(){
			window.open("/index.php?a=dict&m=export");
		});
		$("body").on("click", ".btn_del", function(){
			var id = $(this).data("id");
			var that = this;
			$(".msgdialog").html($(this).data("confirm")).dialog({
				modal: true, buttons: {
					Ok: function(){
						$(".msg").html("");
						$.ajax({type: "POST", dataType: "JSON", url: "/index.php?a=dict&m=delete&t=ajax", data: {id: id}, async: false, success: function(result){
							if(result.state){
								$.fn.notificationShow($(".msg"), "删除成功。", "success");
								$(".tr_" + id).remove();
								$(that).parent().parent().remove();
							}else{
								$.fn.notificationShow($(".msg"), "删除失败。", "error");
							}
						}, error: function(jqXHR, textStatus, errorThrown){
							$.fn.notificationShow($(".msg"), "AJAX请求出错[" + errorThrown + "].", "error");
						}});
						$(this).dialog("close");
					},
					Cancel: function(){
						$(this).dialog("close");
					}
				}
			});
		});
		$(".btn_clear").click(function(){
			$(".msg").html("");
			$.ajax({type: "POST", dataType: "JSON", url: "/index.php?a=dict&m=clear&t=ajax", async: false, success: function(result){
				if(result.state){
					$.fn.notificationShow($(".msg"), "清空缓存成功。", "success");
				}else{
					$.fn.notificationShow($(".msg"), "清空缓存失败。", "error");
				}
			}, error: function(jqXHR, textStatus, errorThrown){
				$.fn.notificationShow($(".msg"), "AJAX请求出错[" + errorThrown + "].", "error");
			}});
		});
		// 文件拖拽
		function F_DropType(obj, option){
			if(option == "stop"){
				obj.get(0).removeEventListener("drop", H_Drop, false);
				obj.unbind("dragleave", H_DragLeave);
				obj.unbind("dragover", H_DragOver);
				obj.get(0).addEventListener("drop", H_DropWait, false);
				obj.bind("dragleave", H_DropWait);
				obj.bind("dragover", H_DropWait);
			}else if(option == "start"){
				obj.get(0).removeEventListener("drop", H_DropWait, false);
				obj.unbind("dragleave", H_DropWait);
				obj.unbind("dragover", H_DropWait);
				obj.get(0).addEventListener("drop", H_Drop, false);
				obj.bind("dragleave", H_DragLeave);
				obj.bind("dragover", H_DragOver);
			}
		}
		function H_DropWait(event){
			event.stopPropagation();
			event.preventDefault();
		}
		function H_Drop(event){
			F_DropType($(this), "stop");
			event.stopPropagation();
			event.preventDefault();
			$(this).css({"border-color":"#DDDDDD"});
			var filelist = event.dataTransfer.files;
			if (!filelist || !filelist.length){
				var msg_obj = $(this).parent().find(".message");
				msg_obj.css("z-index", $.fn.dyMaxZindex("*:visible")).html("出错。").show(300);	
				$("body").one('mousedown', function(){msg_obj.hide(300);});
				F_DropType($(this), "start");
			}else if(filelist.length > 1){
				var msg_obj = $(this).parent().find(".message");
				msg_obj.css("z-index", $.fn.dyMaxZindex("*:visible")).html("仅限一个文件。").show(300);	
				$("body").one('mousedown', function(){msg_obj.hide(300);});
				F_DropType($(this), "start");
			}else{
				var that = this, obj = $(this);
				obj.html("<div class='info'><div class='loading'><div class='bar'></div></div></div>");
				$.fn.dyUploadFile(filelist[0], {
					dataType: "JSON",
					name: "dict",
					url: "/index.php?a=dict&m=import&t=ajax",
					onEvent: {success: function(result){
						if(result.state){
							$(".msgdialog").attr("title", "消息").html(result.message).dialog({
								modal: true, buttons: {
									Ok: function(){
										window.location.reload();
									}
								}
							});
						}else{
							obj.html("<div class='info'>拖拽文件到此</div>");
							var msg_obj = $(that).parent().find(".message");
							msg_obj.css("z-index", $.fn.dyMaxZindex("*:visible")).html(result.message).show(300);	
							$("body").one('mousedown', function(){msg_obj.hide(300);});
						}
						F_DropType($(that), "start");
					}, error: function(error){
						var msg_obj = $(this).parent().find(".message");
						msg_obj.css("z-index", $.fn.dyMaxZindex("*:visible")).html("AJAX请求出错[" + error + "].").show(300);	
						$("body").one('mousedown', function(){msg_obj.hide(300);});
						F_DropType($(that), "start");
					}, progress: function(event){
						obj.find(".loading .bar").animate({"width": (event.loaded / filelist[0].size) * 100 + "%"}, 300);
					}}
				});
			}
		}
		function H_DragLeave(event){
			event.stopPropagation();
			event.preventDefault();
			$(this).css({"border-color":"#DDDDDD"});
		}
		function H_DragOver(event){
			event.stopPropagation();
			event.preventDefault();
			$(this).css({"border-color":"#4D90FE"});
		}
	{/literal}
	</script>
{include file="common_copyright.html"}
{include file="common_bottom.html"}
